package exam.uit;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SQLiteExample extends Activity implements OnClickListener{

	Button sqlUpdate, sqlView, sqlDelete, sqlModify, sqlGetInfo , sqlInfoFull;
	EditText sqlName, sqlHotness, sqlRow ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sqliteexample);
		sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
		sqlName = (EditText) findViewById(R.id.etSQLName);
		sqlHotness = (EditText) findViewById(R.id.etSQLHotness);
		sqlView = (Button) findViewById(R.id.bSQLopenView);
		
		sqlRow = (EditText) findViewById(R.id.etSQLrowInfo);
		sqlGetInfo = (Button) findViewById(R.id.bSQLrowInfo);
		sqlModify = (Button) findViewById(R.id.bSQLmodify);
		sqlDelete = (Button) findViewById(R.id.bSQLdelete);
		sqlInfoFull = (Button) findViewById(R.id.bInfoFull);
		
		
		final TextView tvName = (TextView) findViewById(R.id.tvName);
		final TextView tvInfo = (TextView) findViewById(R.id.tvInfo);
		
		sqlView.setOnClickListener(this);
		sqlUpdate.setOnClickListener(this);
		sqlGetInfo.setOnClickListener(this);
		sqlModify.setOnClickListener(this);
		sqlDelete.setOnClickListener(this);
		sqlInfoFull.setOnClickListener(this);
	}

	//@Override
	public void onClick(View arg0) {
		switch (arg0.getId()){
		case R.id.bSQLUpdate:
			boolean didItwork = true;
			try{
				String name = sqlName.getText().toString();
				String hotness = sqlHotness.getText().toString();
				
				HotOrNot entry = new HotOrNot(SQLiteExample.this);
				entry.open();
				entry.createEntry(name, hotness);
				entry.close();
			}catch (Exception e) {
				didItwork = false;
				String error = e.toString();
				Dialog d = new Dialog(this);
				d.setTitle("Dam it!");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();
			}finally{
				if(didItwork){	
					Dialog d = new Dialog(this);
					d.setTitle("Hey Yea!");
					TextView tv = new TextView(this);
					tv.setText("Success");
					d.setContentView(tv);
					d.show();
				}
			}
			
			
			break;
					
					
		case R.id.bSQLopenView:
			//setContentView(R.layout.sqlview);
			//TextView test = (TextView) findViewById(R.id.tvSQLinfo);
			//test.setText("loveu");
			
			Intent i = new Intent("exam.uit.SQLVIEW");
			startActivity(i);
			break;
			
		case R.id.bSQLrowInfo:
			try{
				String s = sqlRow.getText().toString();
				long l = Long.parseLong(s);
				HotOrNot hon = new HotOrNot(this);
				hon.open();
				String returnName = hon.getName(l);
				String returnHotness = hon.getHotness(l);
				hon.close();
				
				sqlName.setText(returnName);
				sqlHotness.setText(returnHotness);
			}catch (Exception e) {
				
				String error = e.toString();
				Dialog d = new Dialog(this);
				d.setTitle("Dam it!");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();
			}
			break;
			
			
		case R.id.bSQLmodify:
		try{
			String sRow = sqlRow.getText().toString();
			String sName = sqlName.getText().toString();
			String sHotness = sqlHotness.getText().toString();
			long lRow = Long.parseLong(sRow);
			HotOrNot ex = new HotOrNot(this);
			ex.open();
			ex.updateEntry(lRow , sName, sHotness);
			ex.close();
		}catch (Exception e) {
			String error = e.toString();
			Dialog d = new Dialog(this);
			d.setTitle("Dam it!");
			TextView tv = new TextView(this);
			tv.setText(error);
			d.setContentView(tv);
			d.show();		
		}	
			break;
			
			
		case R.id.bSQLdelete:
			try{
				String s1 = sqlRow.getText().toString();
				long l1 = Long.parseLong(s1);
				HotOrNot ex1 = new HotOrNot(this);
				ex1.open();
				ex1.deleteEntry(l1);
				ex1.close();
			}catch (Exception e) {
				String error = e.toString();
				Dialog d = new Dialog(this);
				d.setTitle("Dam it!");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();
			}
			break;
			
		case R.id.bInfoFull:
			String f = sqlRow.getText().toString();
			long f1 = Long.parseLong(f);
			HotOrNot info = new HotOrNot(this);
			info.open();
			String nameInfo = info.getName(f1);
			String HotnessInfo = info.getHotness(f1);
			setContentView(R.layout.list);
			
			info.close();
			break;
			
		}
	}

}
